{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "17dc080e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：1000\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：0\n",
      "您购买的商品已增加到购物车~~您的余额还有： 701\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：1\n",
      "您的余额不足，请更换支付方式~~\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：2\n",
      "您购买的商品已增加到购物车~~您的余额还有： 673\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：3\n",
      "您购买的商品已增加到购物车~~您的余额还有： 593\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：q\n",
      "您的购物车:  [['自行车', 299], ['NFT 盲盒', 28], ['徐老师的Python书', 80]]\n",
      "您的余额:  593\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"自行车\",299],\n",
    "    [\"Iphone 14 pro\",8000],\n",
    "    [\"NFT 盲盒\",28],\n",
    "    [\"徐老师的Python书\",80]\n",
    "]\n",
    "shopping_car = []\n",
    "Alipay = int (input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"---------商品列表---------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品编号：\"+str(i),product_list[i])\n",
    "    user_chioce = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户输入了商品编号：shopping\n",
    "    if user_chioce.isdigit():\n",
    "        # 判断钱够不够，商品是谁？==》product_list[user_chioce][1]\n",
    "        if Alipay >= product_list[int(user_chioce)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_chioce)][1]  #买完商品剩余的钱\n",
    "            shopping_car.append(product_list[int(user_chioce)])\n",
    "            print(\"您购买的商品已增加到购物车~~您的余额还有：\",Alipay)\n",
    "        else:\n",
    "           print(\"您的余额不足，请更换支付方式~~\")\n",
    "     # 情景2：用户输入“q”，推出购买\n",
    "    elif user_chioce == \"q\":\n",
    "        print(\"您的购物车: \",shopping_car)\n",
    "        print(\"您的余额: \",Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"您输入的商品不存在或输入有误，请重新尝试\")\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "fa4549eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：100000000\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：4\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [83]\u001b[0m, in \u001b[0;36m<cell line: 10>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[38;5;66;03m# 情景1：用户输入了商品编号：shopping\u001b[39;00m\n\u001b[0;32m     15\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m user_chioce\u001b[38;5;241m.\u001b[39misdigit():\n\u001b[0;32m     16\u001b[0m     \u001b[38;5;66;03m# 判断钱够不够，商品是谁？==》product_list[user_chioce][1]\u001b[39;00m\n\u001b[1;32m---> 17\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m Alipay \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[43mproduct_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43muser_chioce\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m[\u001b[38;5;241m1\u001b[39m]:\n\u001b[0;32m     18\u001b[0m         Alipay \u001b[38;5;241m=\u001b[39m Alipay \u001b[38;5;241m-\u001b[39m product_list[\u001b[38;5;28mint\u001b[39m(user_chioce)][\u001b[38;5;241m1\u001b[39m]  \u001b[38;5;66;03m#买完商品剩余的钱\u001b[39;00m\n\u001b[0;32m     19\u001b[0m         shopping_car\u001b[38;5;241m.\u001b[39mappend(product_list[\u001b[38;5;28mint\u001b[39m(user_chioce)])\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"自行车\",299],\n",
    "    [\"Iphone 14 pro\",8000],\n",
    "    [\"NFT 盲盒\",28],\n",
    "    [\"徐老师的Python书\",80]\n",
    "]\n",
    "shopping_car = []\n",
    "Alipay = int (input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"---------商品列表---------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品编号：\"+str(i),product_list[i])\n",
    "    user_chioce = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户输入了商品编号：shopping\n",
    "    if user_chioce.isdigit():\n",
    "        # 判断钱够不够，商品是谁？==》product_list[user_chioce][1]\n",
    "        if Alipay >= product_list[int(user_chioce)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_chioce)][1]  #买完商品剩余的钱\n",
    "            shopping_car.append(product_list[int(user_chioce)])\n",
    "            print(\"您购买的商品已增加到购物车~~您的余额还有：\",Alipay)\n",
    "        else:\n",
    "           print(\"您的余额不足，请更换支付方式~~\")\n",
    "     # 情景2：用户输入“q”，推出购买\n",
    "    elif user_chioce == \"q\":\n",
    "        print(\"您的购物车: \",shopping_car)\n",
    "        print(\"您的余额: \",Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"您输入的商品不存在或输入有误，请重新尝试\")\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "8e2d1bf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：100\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：0\n",
      "您的余额不足，请更换支付方式~~\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：1\n",
      "您的余额不足，请更换支付方式~~\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：2\n",
      "您购买的商品已增加到购物车~~您的余额还有： 72\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：3\n",
      "您的余额不足，请更换支付方式~~\n",
      "---------商品列表---------\n",
      "商品编号：0 ['自行车', 299]\n",
      "商品编号：1 ['Iphone 14 pro', 8000]\n",
      "商品编号：2 ['NFT 盲盒', 28]\n",
      "商品编号：3 ['徐老师的Python书', 80]\n",
      "请输入商品编号：q\n",
      "您的购物车:  [['NFT 盲盒', 28]]\n",
      "您的余额:  72\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"自行车\",299],\n",
    "    [\"Iphone 14 pro\",8000],\n",
    "    [\"NFT 盲盒\",28],\n",
    "    [\"徐老师的Python书\",80]\n",
    "]\n",
    "shopping_car = []\n",
    "Alipay = int (input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"---------商品列表---------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品编号：\"+str(i),product_list[i])\n",
    "    user_chioce = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户输入了商品编号：shopping\n",
    "    if user_chioce.isdigit():\n",
    "        # 判断钱够不够，商品是谁？==》product_list[user_chioce][1]\n",
    "        if Alipay >= product_list[int(user_chioce)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_chioce)][1]  #买完商品剩余的钱\n",
    "            shopping_car.append(product_list[int(user_chioce)])\n",
    "            print(\"您购买的商品已增加到购物车~~您的余额还有：\",Alipay)\n",
    "        else:\n",
    "           print(\"您的余额不足，请更换支付方式~~\")\n",
    "     # 情景2：用户输入“q”，推出购买\n",
    "    elif user_chioce == \"q\":\n",
    "        print(\"您的购物车: \",shopping_car)\n",
    "        print(\"您的余额: \",Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"您输入的商品不存在或输入有误，请重新尝试\")\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61ca202f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
